package com.ucar.achievement.util;

import com.ucar.achievement.callback.CustomPaginationBuilderCallback;
import com.ucar.achievement.form.AchieveAdjustForm;
import com.ucar.achievement.form.AchievementPostParam;
import com.ucar.achievement.form.Pagination;
import com.zuche.framework.dao.util.QueryCond;
import org.jmesa.limit.Limit;
import org.jmesa.limit.RowSelect;
import org.springframework.stereotype.Component;

/**
 * Description: 该组件类用于创建form对象的Builder<br/>
 * @version V1.0  by 石冬冬-Heil Hitler on  2017/5/31 13:55
 */
@Component
public final class AchievementFormBuilder {

    private final RowSelect DEFAULT_ROW_SELECT = new RowSelect(1, 20, Integer.MAX_VALUE);

    /**
     * 构建一个AchieveAdjustForm对象
     * @param builderCallback
     * @return
     */
    public final AchieveAdjustForm buildAchieveAdjustForm(final CustomPaginationBuilderCallback<AchieveAdjustForm> builderCallback){
        return buildForm(builderCallback,new AchieveAdjustForm());
    }

    /**
     * 构建一个AchievementPostParam对象
     * @param builderCallback
     * @return
     */
    public final AchievementPostParam buildAchievementPostParam(CustomPaginationBuilderCallback<AchievementPostParam> builderCallback){
         return buildForm(builderCallback,new AchievementPostParam());
    }

    /**
     * 构建form
     * @param builderCallback
     * @param form
     * @param <T>
     * @return
     */
    private <T extends QueryCond> T buildForm(CustomPaginationBuilderCallback<T> builderCallback, T form){
        builderCallback.before(form);
        Limit limit = builderCallback.getLimit();
        limit.setRowSelect(DEFAULT_ROW_SELECT);
        form.setLimit(limit);
        return form;
    }

}
